<?php

namespace App\Library\Encrypt;

/**
 * 可逆的加密
 */
class Reversible
{
    const KEY = 1;

    // 加密
    public static function encode(string $txt): string
    {
        for ($i = 0; $i < strlen($txt); $i++) {
            $txt[$i] = chr(ord($txt[$i]) + self::KEY);
        }
        return urlencode(base64_encode(urlencode($txt)));
    }

    // 解密
    public static function decode(string $txt): string
    {
        $txt = urldecode(base64_decode($txt));
        for ($i = 0; $i < strlen($txt); $i++) {
            $txt[$i] = chr(ord($txt[$i]) - self::KEY);
        }
        return $txt;
    }
}
